/////////////////
//  PREAMBLE  //
////////////////


/*	program:	Analysis-2-NC-Roll-Calls.do
	author:		David Bateman */

/******************************************************************************/
/***                                                                        ***/
/*** File provides an analysis of ideal points in the NC State legislature  ***/
/***                                                                        ***/
/*** Members of the House of Commons and Senate were elected from the same  ***/
/*** district (excepting a few multi-county senatorial districts ) at the   ***/
/*** same time.	Senators were elected under a high property qualification   ***/
/*** while commoners were elected under a low poll tax (near free adult male***/
/*** suffrage (whites only after 1835)                                      ***/
/***                                                                        ***/
/*** The ideal points were estimated in R using the pscl IDEAL package      ***/
/***                                                                        ***/
/*** There are two ideal point files. The first	estimates a roll call for   ***/
/*** each legislator for each session of the NC Assembly. The House and     ***/
/*** Senate scores were projected into the same space by merging their      ***/
/*** roll call matrices by identical votes in the House / Senate. Those     ***/
/*** scores are comparable across chambers, but not necessarily across      ***/
/*** sessions. The second file first merges the chambers as described above ***/
/*** and then uses members who served in multiple sessions of the same      ***/
/*** chamber (and from the same district) as bridges.                       ***/
/***                                                                        ***/
/*** I estimate the following model of ideal points.                        ***/
/*** I_it =�Q_it + a_d + e_idt                                              ***/
/***                                                                        ***/
/******************************************************************************/

*** Session-by-Session (common space across chambers) ideal points
clear
eststo drop est*
cd "..\..\Dataverse\"
set more off

*** This file has estimates of cross-institutional common space scores
*** for each legislative session. The different sessions are not in a common 
*** space. 

insheet using "Data\combined-session-by-session.csv" 
rename icpsrlegis id1

label variable state "State abbreviation"
label variable session1 "Session of the NC Assembly"
label variable icpsrstate "ICPSR state code"
rename cd dist /* The IDEAL code used the readKH function, which assumes congressional districts*/
label variable dist  "State legislative district (arbitrarily assigned by author)" 
label variable id1 "Legislator identifier"
label variable party "Party name"
label variable partycode "Voteview party codes"
label variable name "Legislator name"
label variable d1 "Estimated 1st Dimension ideal point"
label variable d11 "Standard deviation in estimated ideal point"
label variable lowerd1 "Lower bound of 2.5% of sample ideal point estimates"
label variable upperd1 "Upper bound of 97.5% of sample ideal point estimates"


*** Merge legislator info
merge 1:m session1 id1 using "Data\leg-info-nc.dta"

*** Generate indicator for Democratic party affiliation
gen dem = partycode==100

*** Generate county / session id. Fixed effect model will compare ideal points
*** between chambers for same county/same session.
egen group = group(county session1)
xtset group

label define chamber 1 "Lower Threshold" 2 "Higher Threshold"
label define dem 0 "Non-Democratic" 1 "Democratic"
label value dem dem 
label value chamber chamber 

*** Model 1 - Individual ideal points across chambers
eststo main_1: xtreg d1 i.chamber if session1<=71, fe robust
estadd local ctrls "No"
estadd local fixed "Yes"
*** Model 2 - Individual ideal points across chambers by party. 
eststo suppl_1: xtreg d1 i.dem##i.chamber if session1<=71, fe robust
estadd local ctrls "No"
estadd local fixed "Yes"

margins, over(i.chamber i.dem)
marginsplot, recast(scatter) name(graph1, replace) xsc(reverse)	///
	plot1opts(msy(O) mlw(thin) mlc(black) mfc(white) msize(large))			///
	plot2opts(msy(T) mlw(thin) mlc(black) mfc(gray) msize(large))			///
	ci1opts(lw(thin) lc(black))	 ci2opts(lw(thin) lc(black))  ///
	graphreg(col(none)) xtitle("")  xlab(, angle(h) nogrid labsize(small))	///
	ytitle("Ideal Point Scale", size(small)) ylab(, nogrid angle(h) labsize(small))		///
	legend(size(small)) title("Session-by-Session", size(small) col(black)) 

********************************************************************************
*** Jointly estimated ideal points (roll calls bridge chambers, legislators bridge session) 

clear
insheet using "Data\common-space-merged-ideal-points.csv" 
*** Create label variables.
label variable state "State abbreviation"
label variable icpsrstate "ICPSR state code"
rename cd dist /* The IDEAL code used the readKH function, which assumes congressional districts*/
label variable dist  "State legislative district (arbitrarily assigned by author)" 
rename rowname member
label variable member "Member name"
rename party partyname 
label variable partyname "Party name"
label variable d1 "Estimated 1st Dimension ideal point"
label variable d11 "Standard deviation in estimated ideal point"
label variable lowerd1 "Lower bound of 2.5% of sample ideal point estimates"
label variable upperd1 "Upper bound of 97.5% of sample ideal point estimates"

*** The IDEAL code used the readKH function, which loads the ID column as the ICPSR Congressional Legislator ID. The ID here is assigned by the author.
*** The variable id1 is distinct for each legislator. The variable newid is distinct for each legislator+chamber (a legislator who served in the House 
*** and Senate will have a unique id1 value but two newid values). 
rename icpsrlegis newid 
label variable newid "Legislator ID, by chamber"

*** The merged ideal point file does not identify the chamber. 
merge 1:m newid  using "Data\leg-info-nc.dta"

*** Drop the post-reform sessions.
drop if session1==71 | session1==72

*** Generate indicator for Democratic party affiliation
gen dem  = partycode==100

*** Generate county / session id. Fixed effect model will compare ideal points
*** between chambers for same county/same session.
egen group = group(county session1)
xtset group

label define chamber 1 "Lower Threshold" 2 "Higher Threshold"
label define dem 0 "Non-Democratic" 1 "Democratic"
label value dem dem 
label value chamber chamber 

*** Model 1 - Individual ideal points across chambers
eststo main_2: xtreg d1 i.chamber , fe robust
estadd local ctrls "No"
estadd local fixed "Yes"
*** Model 2 - Individual ideal points across chambers by party. 
eststo suppl_2: xtreg d1 i.dem##i.chamber , fe robust
estadd local ctrls "No"
estadd local fixed "Yes"

margins, over(i.chamber i.dem)
marginsplot, recast(scatter) name(graph2, replace) xsc(reverse)	///
	plot1opts(msy(O) mlw(thin) mlc(black) mfc(white) msize(large))			///
	plot2opts(msy(T) mlw(thin) mlc(black) mfc(gray) msize(large))			///
	ci1opts(lw(thin) lc(black))	 ci2opts(lw(thin) lc(black))  ///
	graphreg(col(none)) xtitle("")  xlab(, angle(h) nogrid labsize(small))	///
	ytitle("Ideal Point Scale", size(small)) ylab(, nogrid angle(h) labsize(small))		///
	legend(size(small)) title("Jointly Estimated Sessions", size(small) col(black)) 

	
********************************************************************************
*** Analysis of specific votes. 
use "Data\leg-info-nc.dta", clear
rename session1 congress
duplicates drop id1, force


forvalues k = 46(2)56 {
	loc l = `k'+1
	qui merge 1:1 id1 using "Data\matrices\Joint18`k'-`l'.dta", nogen
	
	qui drop Hou* Sen*
	qui foreach x of varlist cmerge* {
		loc iter = `iter'+1
		replace `x' = 0 if `x'==6
		rename `x' merged`iter'
	}
}
reshape long merged, i(id1) j(rc)


*** Each of the bridge roll calls has been coded according to a broad issue area.
*** I input those issue areas here.

***	1,2,10 						= National Politics
***	3,40,57 					= Banking
***	4 							= Municipalities
***	5,8,12,26,27,38,44,45, 		
*** 46,49,56,58,60				= Internal Improvements
***	6,9,34,48,53,55 			= Misc
***	7,11,13,14,33,35,39			= Counties
***	15-22,41,42,54				= Slavery
***	23,24,29,30,31,32,37,43,
*** 50,51,52,					= Constitution
***	25							= Women
***	28,47,59					= Revenue
***	36							= Schools

gen issue 		= 1 if rc==1 | rc==2 | rc==10
replace issue 	= 2 if rc==3 | rc==40 | rc==57
replace issue	= 3 if rc==5 | rc==8 | rc==12 | rc==26 | rc==27	| rc==38 | rc==44 | rc==45 | rc==46 | rc==49 | rc==56 | rc==58 | rc==60
replace issue	= 4 if rc==7 | rc==11 | rc==13 | rc==14 | rc==33 | rc==35 | rc==39
replace issue 	= 5 if rc>=15 & rc<=22 | rc==41 | rc==42 | rc==54
replace issue 	= 6 if rc==23 | rc==24 | rc==29 | rc==30 | rc==31 | rc==32 | rc==37	| rc==43 | rc==50 | rc==51 | rc==52
replace issue 	= 7 if rc==28 | rc==47 | rc==59
replace issue	= 8 if rc==4 | rc==25 | rc==36 | rc==6 | rc==9 | rc==34 | rc==48 | rc==53 | rc==55

*** Generate indicator for Democratic party affiliation
gen dem  = partycode==100
		
capture label drop issue
label define issue 1 "National Politics"
label define issue 2 "Banking", add
label define issue 3 "Internal Improvements", add
label define issue 4 "Counties", add
label define issue 5 "Slavery", add
label define issue 6 "Constitution", add
label define issue 7 "Revenue", add
label define issue 8 "Misc.", add
label values issue issue

label define chamber 1 "Lower Threshold" 2 "Higher Threshold"
label define dem 0 "Non-Democratic" 1 "Democratic"
label value dem dem 
label value chamber chamber 

egen group = group(county)
xtset group

eststo suppl_rc1: xtreg merged i.chamber, fe robust
estadd local ctrls "No"
estadd local fixed "Yes"
eststo suppl_rc2: xtreg merged i.chamber##i.issue, fe robust
estadd local ctrls "No"
estadd local fixed "Yes"

margins, over(i.issue i.chamber )
marginsplot, recast(scatter) name(graph3, replace) 	///
	plot1opts(msy(O) mlw(thin) mlc(black) mfc(white) msize(large))			///
	plot2opts(msy(T) mlw(thin) mlc(black) mfc(gray) msize(large))			///
	ci1opts(lw(thin) lc(black))	 ci2opts(lw(thin) lc(black))  ///
	graphreg(col(white)) xtitle("")  xlab(, angle(h) nogrid labsize(small) alternate)	///
	ytitle("Probability of Voting Yea", size(small)) ylab(, nogrid angle(h) labsize(small))		///
	legend(size(small)) title("", size(small) col(black)) 

	
	

********************************************************************************
*****************		Combine Figures and tables		************************
********************************************************************************

****************				Main Text 				************************

esttab main_1 main_2 using "Tables\Table-4.tex", se(a2)  label 	///
	title(Effect of Senate Property Qualification on Member Ideal Points) 	///
	drop(1.chamber _cons)	///
	mgroups("Session-by-Session" "Jointly Estimated", pattern(1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span )         ///
	mtitles("Ideal Points" "Ideal Points" ) nogaps nodepvars nostar compress noconstant nonotes	 nonumbers		///
	stats(ctrls fixed N_g N, fmt(%9.0g) label("Additional controls" "County/year fixed effects" "Counties" "N")) b(a2) 						///
	nolines prehead(\begin{table}[hbt!]\centering	\caption{@title} \label{tab:4} \begin{tabular}{l*{@M}{c}} \toprule)		///		
	posthead(\midrule)  prefoot(\midrule) ///
	postfoot(\bottomrule \end{tabular} \floatnote{Robust standard errors in parentheses} \end{table})   tex    replace

graph display graph3
graph export "Figures\Figure-4.pdf", replace
	
****************		Supplemental Material			************************
*** Table A14: Effect of Senate Property Qualification on Member Ideal Points
esttab main_1 suppl_1 main_2 suppl_2 using "Tables\Supplemental\Supplemental-Table-14.tex", se(a2)  label 	///
	title(Effect of Senate Property Qualification on Member Ideal Points) 	///
	drop(1.chamber _cons 0.dem 0.dem#1.chamber 0.dem#2.chamber 1.dem#1.chamber )	///
	mgroups("Session-by-Session" "Jointly Estimated", pattern(1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span )         ///
	nomtitles nogaps nodepvars nostar compress noconstant nonotes	 nonumbers		///
	stats(ctrls fixed N_g N, fmt(%9.0g) label("Additional controls" "County/year fixed effects" "Counties" "N")) b(a2) 						///
	note("Robust standard errors in parentheses.") 														///
	nolines prehead(\begin{table}[hbt!]\centering	\caption{@title} \label{tab:tableA14} \begin{tabular}{l*{@M}{c}} \toprule)		///		
	posthead(\midrule)  prefoot(\midrule) ///
	postfoot(\bottomrule \end{tabular} \floatnote{Robust standard errors in parentheses}  \end{table})   tex    replace
	
*** Table A14: Effect of Senate Property Qualification on Legislators' Vote Choice
esttab suppl_rc1 suppl_rc2 using "Tables\Supplemental\Supplemental-Table-15.tex", se(a2)  label 	///
	title(Effect of Senate Property Qualification on Legislators' Vote Choice) 	///
	drop(1.chamber 1.issue _cons 1.chamber#1.issue 1.chamber#2.issue 1.chamber#3.issue 1.chamber#4.issue 1.chamber#5.issue 1.chamber#6.issue 1.chamber#7.issue 1.chamber#8.issue 2.chamber#1.issue)	///
	mtitles("Vote Choice" "Vote Choice" ) nogaps nodepvars nostar compress noconstant nonotes nonumbers		///
	stats(ctrls fixed N_g N, fmt(%9.0g) label("Additional controls" "County/year fixed effects" "Counties" "N")) b(a2) 						///
	nolines prehead(\begin{table}[hbt!]\centering	\caption{@title} \label{tab:tableA15} \begin{tabular}{l*{@M}{c}} \toprule)		///		
	posthead(\midrule)  prefoot(\midrule) ///
	postfoot(\bottomrule \multicolumn{3}{l}{\footnotesize @note} \end{tabular} \floatnote{Robust standard errors in parentheses} \end{table})   tex    replace

*** Figure A9: North Carolina Legislator Ideal Points, by Party
grc1leg graph1 graph2, graphreg(col(white)) ycommon	///
		title("North Carolina Legislator Ideal Points", size(small) col(black))
graph export "Figures\Supplemental\Supplemental-Figure-9.pdf", replace	

